package com.tracking.locon;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ChatArrayAdapter extends ArrayAdapter<ChatComment> {

	private TextView mTextBubble;
	private TextView mTimeStamp;
	private List<ChatComment> mBubbles = new ArrayList<ChatComment>();
	//private RelativeLayout mWrapper;
	public String mComment;
	private Context mContext;
	
	@Override
	public void add(ChatComment object) {
		mBubbles.add(object);
		super.add(object);
	}

	public ChatArrayAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		mContext = context;
	}

	public int getCount() {
		return this.mBubbles.size();
	}

	public ChatComment getItem(int index) {
		return this.mBubbles.get(index);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ChatComment chatComment = getItem(position);
		
		//If it is received message, use the black bubble
		if( getItemViewType(position) == 0){
			if (row == null ) {
				LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = inflater.inflate(R.layout.chat_bubble, parent, false);
			}
			mTextBubble = (TextView) row.findViewById(R.id.tvBubble);
			mTimeStamp = (TextView) row.findViewById(R.id.tvTimeStamp);
		}
		//Else if sent message, use the blue bubble
		else{
			if (row == null ) {
				LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = inflater.inflate(R.layout.chat_bubble_right, parent, false);
			}
			mTextBubble = (TextView) row.findViewById(R.id.tvBubbleRight);
			mTimeStamp = (TextView) row.findViewById(R.id.tvTimeStampRight);
		}
		
		//Set the max width and text for the bubble
		mTextBubble.setMaxWidth((int) (Utility.getPhonePixelWidth(mContext) * 0.75));
		mTextBubble.setText(chatComment.comment);
		mTimeStamp.setText(chatComment.timeStamp);
		
		return row;
	}

	/*
	 * Decode the text bubble into Bitmap
	 */
	public Bitmap decodeToBitmap(byte[] decodedByte) {
		return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
	}
	
	@Override
	public int getItemViewType(int position) {
	    return getItem(position).isFromFriend ? 0 : 1;
	}

	@Override
	public int getViewTypeCount() {
	    return 2;
	}
	
	/*
	ChatComment chatComment = getItem(position);

	mTextBubble = (TextView) row.findViewById(R.id.tvBubble);
	mTextBubble.setText(chatComment.comment);
	mTextBubble.setBackgroundResource(chatComment.isFromFriend ? R.drawable.bubble_left : R.drawable.bubble_right);
	
	mTimeStamp = (TextView) row.findViewById(R.id.tvTimeStamp);
	mTimeStamp.setText(chatComment.timeStamp);
	//setTimeAlignment(chatComment.isFromFriend, mTimeStamp);
	
	mWrapper = (RelativeLayout) row.findViewById(R.id.rlWrapper);
	mWrapper.setGravity(chatComment.isFromFriend ? Gravity.LEFT : Gravity.RIGHT);
*/
}